home *** CD-ROM | disk | FTP | other *** search
/ PC Graphics Unleashed / PC Graphics Unleashed.iso / ch20 / source / hist8.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-12  |  4.8 KB  |  253 lines

  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <conio.h>
  4. #include <graphics.h>
  5. #include <io.h>
  6. #include <dos.h>
  7. #include <malloc.h>
  8. #include <string.h>
  9. #include <math.h>
  10.  
  11.  
  12. #define BUFFERSIZE 128*128
  13.  
  14.  
  15. void main(int argc, char **argv)
  16. {
  17. int i,j,k,l;
  18. FILE *in;
  19. unsigned char buffer[BUFFERSIZE];
  20. int filesize;
  21. int bpf;           /* buffers per file */
  22. unsigned int histogram[256];
  23. int grdriver, grmode, grerror;
  24. float avg, temp;
  25. short disp[512];
  26. char ticks[40];
  27. int tw;
  28. int mousepresent, mousex, mousey;
  29. int oldmousex, oldmousey;
  30. union REGS regs;
  31. unsigned char save[18];
  32. int first = 1;
  33.  
  34.  
  35.  
  36.   /* Ensure proper syntax */
  37.   
  38.   if(argc != 2)
  39.     {
  40.     printf("Usage::  hist8 filename.\n\n");
  41.     exit(1);
  42.     }
  43.  
  44.  
  45.   /* open input file */
  46.   
  47.   in = fopen(argv[1], "rb");
  48.   if(in == NULL)
  49.     {
  50.     printf("Can not open file: %s.\n\n", argv[1]);
  51.     exit(2);
  52.     }
  53.  
  54.   
  55.   /* Clear the histogram */
  56.  
  57.   for(i=0; i<256; i++)
  58.     histogram[i] = 0;
  59.   
  60.   
  61.   /* Determine the number of disk reads needed */
  62.  
  63.   filesize = filelength( fileno(in) );
  64.   bpf = filesize / sizeof(buffer);
  65.  
  66.   
  67.   /* Calculate the histogram */
  68.   
  69.   for(i=0; i<bpf; i++)
  70.     {
  71.     fread(buffer, sizeof(buffer), 1, in);
  72.  
  73.     for(j=0; j<BUFFERSIZE; j++)
  74.       histogram[ buffer[j]]++;
  75.     }
  76.  
  77.   fclose(in);
  78.  
  79.  
  80.   /* check graphics hardware */
  81.  
  82.   grdriver = DETECTX;
  83.   grmode = 0;
  84.   detectgraph(&grdriver, &grmode);
  85.   switch(grdriver)
  86.     {
  87.     case VESA256:
  88.     case ATI256:
  89.     case COMPAQ:
  90.     case TSENG3256:
  91.     case TSENG4256:
  92.     case GENOA5:
  93.     case GENOA6:
  94.     case OAK:
  95.     case PARADIS256:
  96.     case TECMAR:
  97.     case TRIDENT256:
  98.     case VIDEO7:
  99.     case VIDEO7II:
  100.       break;
  101.  
  102.     default:
  103.       printf("Unable to detect 256 color graphics adapter.\n");
  104.       exit(5);
  105.       break;
  106.     }
  107.   
  108.   
  109.   grmode = 1;
  110.   initgraph(&grdriver, &grmode, "");
  111.   grerror = graphresult();
  112.   if(grerror)
  113.     {
  114.     closegraph();
  115.     printf("Error %d initializing graphics mode.\n", grerror);
  116.     exit(6);
  117.     }
  118.  
  119.  
  120.   /* Initialize mouse */
  121.   
  122.   regs.w.ax = 0;
  123.   int386(0x33, ®s, ®s);
  124.  
  125.   if((short)regs.w.ax != -1)
  126.     mousepresent = 0;
  127.    else mousepresent = 1;
  128.  
  129.  
  130.   /* Setup mouse range and turn on cursor */
  131.   
  132.   if(mousepresent) 
  133.     {
  134.     regs.w.ax = 0x0007;  /* horizontal range */
  135.     regs.w.cx = 64;
  136.     regs.w.dx = 578;     /* 64 -> 578 */
  137.     int386(0x33, ®s, ®s);
  138.  
  139.     regs.w.ax = 0x0008;  /* vertical range */
  140.     regs.w.cx = 40;
  141.     regs.w.dx = 440;     /* 40 -> 440 */
  142.     int386(0x33, ®s, ®s);
  143.    
  144.     regs.w.ax = 0x0001;  /* display cursor */
  145.     int386(0x33, ®s, ®s);
  146.     
  147.     oldmousex = 0;
  148.     oldmousey = 0;
  149.     }
  150.   
  151.  
  152.   /* get the average histogram value */
  153.  
  154.   avg = 0.0;
  155.   j = 0;
  156.  
  157.   for(i=0; i<256; i++)
  158.     {
  159.     avg += (float)histogram[i];
  160.     if(histogram[i] != 0)
  161.       j++;
  162.     }
  163.  
  164.   avg = avg/(float)j;
  165.  
  166.  
  167.   /* setup display array */
  168.  
  169.   for(i=0; i<256; i++)
  170.     {
  171.     k = (int)floor( (double)(histogram[i] / avg * 200.0));
  172.     if(k > 400)
  173.       k = 400;
  174.     disp[i*2] = k;
  175.     disp[i*2 + 1] = k; 
  176.     }
  177.  
  178.  
  179.   /* display background */
  180.   
  181.   setbkcolor(31);     /* white */
  182.   cleardevice();
  183.   
  184.   setcolor(0);
  185.   rectangle(63,39, 640-64,480-39);
  186.  
  187.  
  188.   /* display histogram */
  189.   
  190.   setcolor(1);        /* blue */
  191.   for(i=0; i<512; i++)
  192.     line(64+i, 440, 64+i, 440 - disp[i]);
  193.   
  194.   
  195.   /* display tick marks and title */
  196.   
  197.   setcolor(0);
  198.   for(i=0; i<=8; i++)  
  199.     {
  200.     sprintf(ticks, "%d", i*32);
  201.     tw = textwidth(ticks);
  202.     outtextxy(64*(i+1)-tw/2+1, 450, ticks);
  203.     line(64*(i+1), 441, 64*(i+1), 444);
  204.     }
  205.  
  206.   tw = textwidth(argv[1]);
  207.   outtextxy(320-tw/2+1, 20, argv[1]);
  208.   setfillstyle(SOLID_FILL, 31);
  209.  
  210.  
  211.   /* wait for keystrike then exit */
  212.  
  213.   while(!kbhit())
  214.     if(mousepresent)
  215.       {
  216.       regs.w.ax = 0x0003;
  217.       int386(0x33, ®s, ®s);
  218.       mousex = (int)regs.w.cx;
  219.       mousey = (int)regs.w.dx;
  220.       if((mousex != oldmousex) || (mousey != oldmousey))
  221.         {
  222.         if(!first)
  223.           {
  224.           for(i=-4; i<=4; i++)
  225.             putpixel(oldmousex+i, oldmousey, save[i+4]);
  226.           for(i=-4; i<=4; i++)
  227.             putpixel(oldmousex, oldmousey+i, save[i+13]);
  228.           }
  229.          else first = 0;
  230.         
  231.         for(i=-4; i<=4; i++)
  232.           save[i+4] = getpixel(mousex+i, mousey);
  233.         for(i=-4; i<=4; i++)
  234.           save[i+13] = getpixel(mousex, mousey+i);
  235.         setcolor(0);
  236.         line(mousex-4, mousey, mousex+4, mousey);
  237.         line(mousex, mousey-4, mousex, mousey+4);
  238.         oldmousex = mousex;
  239.         oldmousey = mousey;
  240.         
  241.         bar(260,465, 450,475);
  242.         sprintf(ticks, "Histogram value:  %3d", (mousex-64)/2);
  243.         outtextxy(260, 465, ticks);
  244.         }
  245.  
  246.       }
  247.     
  248.   
  249.   getch();
  250.   
  251.   closegraph();
  252. }
  253.